package com.conseilimmo.repository;

import com.hildeberto.home.util.EntityManagerHelper;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import objetsBd.BatimentBd;

/**
 *
 * @author verpoorten
 */
public class BatimentBsn {

    private static final Logger LOGGER = Logger.getLogger(BatimentBsn.class.getName());
    private EntityManager em = EntityManagerHelper.EM.open();

    public List<BatimentBd> trouverListeBatiments() {
        String sql = "select b from BatimentBd  b";
        List<BatimentBd> batiments = em.createQuery(sql).getResultList();
        logAfficherBatiments(batiments);
        return batiments;
    }

    public BatimentBd trouverBatimentParId(Long idBatiment) {
        if (idBatiment != null) {
            LOGGER.log(Level.INFO, "trouverBatimentParId :{0}", idBatiment);
            String sql = "select b from BatimentBd  b";
            sql += " where b.idBatiment = :batimentId";
            BatimentBd bat = (BatimentBd)em.createQuery(sql).setParameter("batimentId", idBatiment).getSingleResult();
            afficherBatiment(bat);
            return  bat;
        }
        return null;
    }

    public List<BatimentBd> trouverBatimentParDenomination(String denomination) {
        String sql = "select b from BatimentBd  b";
        sql += " where b.denomination = :denomination";
        return em.createQuery(sql).setParameter("denomination", denomination).getResultList();
    }

    private void logAfficherBatiments(List<BatimentBd> batiments) {
        for (BatimentBd batiment : batiments) {
            LOGGER.log(Level.INFO, "batiment denomination:{0}", batiment.getDenomination());
        }
    }

    public void supprimerBatiment(Long idBatiment) {
        BatimentBd batimentASupprimer = trouverBatimentParId(idBatiment);
        em.remove(batimentASupprimer);
    }

    public BatimentBd ajouterBatiment(BatimentBd nouveauBatiment) {
        BatimentBd nouveauBatimentBd = em.merge(nouveauBatiment);     
        return nouveauBatimentBd;
    }
    public void modifierBatiment(BatimentBd batimentModifie) {
        LOGGER.log(Level.INFO, "modifierBatiment");
        em.merge(batimentModifie);
         
        //relecture
        LOGGER.log(Level.INFO, "relecture");
        BatimentBd bat = trouverBatimentParId(batimentModifie.getIdBatiment());
        afficherBatiment(bat);
       
    }
   

    public void afficherBatiment(BatimentBd batimentModifie) {
        LOGGER.log(Level.INFO, "id:{0}", batimentModifie.getIdBatiment());
        LOGGER.log(Level.INFO, "nom:{0}", batimentModifie.getDenomination());
        LOGGER.log(Level.INFO, "rue:{0}", batimentModifie.getAdresseRue());

    }
    public void testBatiment(){
        
        BatimentBd bat = trouverBatimentParId(1L);
        LOGGER.log(Level.INFO, "testBatiment rue avant:{0}",bat.getAdresseRue());
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");


        Calendar dCal = Calendar.getInstance();
   dCal.setTime(new Date());
 String formatted = format1.format(dCal.getTime());
System.out.println(formatted);
        bat.setAdresseRue("rue "+formatted);
        afficherBatiment(bat);
        em.merge(bat);
       
        afficherBatiment(bat);
        
    }
    public void test(){
        
       BatimentBd bb= trouverBatimentParId(1L);
        bb.setAdresseRue("qsdfqsfqsdfqsf");
        em.merge(bb);

         bb= trouverBatimentParId(1L);
        afficherBatiment(bb);
    }
      
}
